package exttable.timetable.ttpanels;

import exttable.SGTTestParametrs;
import exttable.timetable.models.VisitModel;
import exttable.timetable.models.VisitTableModel;
import utils.Localisation;

import javax.swing.*;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Apr 30, 2010
 * Time: 12:56:15 AM
 * To change this template use File | Settings | File Templates.
 */
public class VisitFillerPanel {

    private JComponent tablePanel;
    private Component visitNumberLabel;
    private JFormattedTextField visitNumber;
    private AbstractButton formTimeSpace;
    private Component enterDescriptionRu;
    private Component enterDescriptionEn;
    private Component enterDescriptionKk;
    private JTextArea jtaRu;
    private JTextArea jtaEn;
    private JTextArea jtaKk;
    private JPanel visitDescriptionPanel;
    private JPanel visitPanel;
    private JScrollPane jScrollSchDescriptionRu;
    private JScrollPane jScrollSchDescriptionEn;
    private JScrollPane jScrollSchDescriptionKk;
    private JScrollPane tableScroll;
    private JTable table;

    public VisitFillerPanel() {
        visitPanel = new JPanel();
        visitDescriptionPanel = new JPanel();
        visitDescriptionPanel.setBorder(BorderFactory.createTitledBorder(Localisation.getMessage("timetable.visits")));

        visitNumberLabel = new JLabel(Localisation.getMessage("timetable.visitamount"));
        visitNumber = new JFormattedTextField(NumberFormat.getNumberInstance());
        visitNumber.setColumns(10);

        formTimeSpace = new JButton(Localisation.getMessage("timetable.entervisittime"));
        enterDescriptionRu = new JLabel(Localisation.getMessage("timetable.schemaNameRu"));
        jtaRu = new JTextArea();
        jtaRu.setColumns(20);
        jtaRu.setRows(3);

        enterDescriptionEn = new JLabel(Localisation.getMessage("timetable.schemaNameEn"));
        jtaEn = new JTextArea();
        jtaEn.setColumns(20);
        jtaEn.setRows(3);

        enterDescriptionKk = new JLabel(Localisation.getMessage("timetable.schemaNameKk"));
        jtaKk = new JTextArea();
        jtaKk.setColumns(20);
        jtaKk.setRows(3);

        jScrollSchDescriptionRu = new JScrollPane();
        tableScroll = new JScrollPane();
        jScrollSchDescriptionRu.setViewportView(jtaRu);

        jScrollSchDescriptionEn = new JScrollPane();
        jScrollSchDescriptionEn.setViewportView(jtaEn);

        jScrollSchDescriptionKk = new JScrollPane();
        jScrollSchDescriptionKk.setViewportView(jtaKk);

        table = new JTable();
        table.setName("visitTable");

        tablePanel = new JPanel();
        tableScroll.setViewportView(table);

        tablePanel.setBorder(BorderFactory.createTitledBorder(Localisation.getMessage("timetable.schemadecription")));
        tablePanel.setLayout(new BorderLayout());
        tablePanel.add(BorderLayout.CENTER, tableScroll);
        tablePanel.setPreferredSize(new Dimension(500, 400));


        formTimeSpace.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (visitNumber.getText().length() == 0) {
                    JOptionPane.showMessageDialog(null, Localisation.getMessage("error.novistinumber"), Localisation.getMessage("error.title"), JOptionPane.ERROR_MESSAGE);
                } else {
                    VisitTableModel vtm = new VisitTableModel(Integer.valueOf(visitNumber.getText()));
                    table.setModel(vtm);
                    table.updateUI();
                }
            }
        });

        visitDescriptionPanel.add(visitNumber);
        visitDescriptionPanel.add(visitNumberLabel);
        visitDescriptionPanel.add(enterDescriptionRu);
        visitDescriptionPanel.add(jScrollSchDescriptionRu);
        visitDescriptionPanel.add(enterDescriptionEn);
        visitDescriptionPanel.add(jScrollSchDescriptionEn);
        visitDescriptionPanel.add(enterDescriptionKk);
        visitDescriptionPanel.add(jScrollSchDescriptionKk);
        visitDescriptionPanel.add(formTimeSpace);
        visitDescriptionPanel.setPreferredSize(new Dimension(500, 250));
        organiseDescriptionLayout();

        visitPanel.add(visitDescriptionPanel);
        visitPanel.add(tablePanel);
        visitPanel.setPreferredSize(new Dimension(510, 620));
        organiseLayout();
    }

    public JPanel getVisitPanel() {
        return visitPanel;
    }

    public Vector<VisitModel> validateTableData() {
        return visitPanelValidator(table.getModel());
    }

    public void stopEditing() {
        if (table.getCellEditor() != null) {
            table.getCellEditor().stopCellEditing();
        }
    }

    private Vector<VisitModel> visitPanelValidator(TableModel model) {

        Vector<VisitModel> toReturn = new Vector<VisitModel>();
        for (int i = 0; i < model.getRowCount(); i++) {
            VisitModel vm = new VisitModel();
            if (model.getValueAt(i, 1) == null & model.getValueAt(i, 2) == null & model.getValueAt(i, 3) == null) {
                JOptionPane.showMessageDialog(null, Localisation.getMessage("error.emptyfileds"), Localisation.getMessage("error.title"), JOptionPane.ERROR_MESSAGE);
                return null;
            }
            vm.setVisit(i);
            vm.setDays(model.getValueAt(i, 1) == null ? 0 : Integer.valueOf(model.getValueAt(i, 1).toString()));
            vm.setHours(model.getValueAt(i, 2) == null ? 0 : Integer.valueOf(model.getValueAt(i, 2).toString()));
            vm.setMinuets(model.getValueAt(i, 3) == null ? 0 : Integer.valueOf(model.getValueAt(i, 3).toString()));
            toReturn.add(vm);
        }
        SGTTestParametrs.getInstance().setSchemaDescRu(jtaRu.getText());
        SGTTestParametrs.getInstance().setSchemaDescEn(jtaEn.getText());
        SGTTestParametrs.getInstance().setSchemaDescKk(jtaKk.getText());
        return toReturn;
    }

    private void organiseDescriptionLayout() {
        SpringLayout layout = new SpringLayout();
        visitDescriptionPanel.setLayout(layout);

        layout.putConstraint(SpringLayout.WEST, visitNumberLabel, 5, SpringLayout.WEST, visitDescriptionPanel);
        layout.putConstraint(SpringLayout.NORTH, visitNumberLabel, 5, SpringLayout.NORTH, visitDescriptionPanel);

        layout.putConstraint(SpringLayout.WEST, visitNumber, 190, SpringLayout.WEST, visitNumberLabel);
        layout.putConstraint(SpringLayout.NORTH, visitNumber, 5, SpringLayout.NORTH, visitDescriptionPanel);

        layout.putConstraint(SpringLayout.WEST, enterDescriptionRu, 5, SpringLayout.WEST, visitDescriptionPanel);
        layout.putConstraint(SpringLayout.NORTH, enterDescriptionRu, 5, SpringLayout.SOUTH, visitNumberLabel);

        layout.putConstraint(SpringLayout.WEST, jScrollSchDescriptionRu, 190, SpringLayout.WEST, enterDescriptionRu);
        layout.putConstraint(SpringLayout.NORTH, jScrollSchDescriptionRu, 5, SpringLayout.SOUTH, visitNumber);

        layout.putConstraint(SpringLayout.WEST, enterDescriptionEn, 5, SpringLayout.WEST, visitDescriptionPanel);
        layout.putConstraint(SpringLayout.NORTH, enterDescriptionEn, 5, SpringLayout.SOUTH, jScrollSchDescriptionRu);

        layout.putConstraint(SpringLayout.WEST, jScrollSchDescriptionEn, 190, SpringLayout.WEST, enterDescriptionRu);
        layout.putConstraint(SpringLayout.NORTH, jScrollSchDescriptionEn, 5, SpringLayout.SOUTH, jScrollSchDescriptionRu);

        layout.putConstraint(SpringLayout.WEST, enterDescriptionKk, 5, SpringLayout.WEST, visitDescriptionPanel);
        layout.putConstraint(SpringLayout.NORTH, enterDescriptionKk, 5, SpringLayout.SOUTH, jScrollSchDescriptionEn);

        layout.putConstraint(SpringLayout.WEST, jScrollSchDescriptionKk, 190, SpringLayout.WEST, enterDescriptionEn);
        layout.putConstraint(SpringLayout.NORTH, jScrollSchDescriptionKk, 5, SpringLayout.SOUTH, jScrollSchDescriptionEn);
               
        layout.putConstraint(SpringLayout.WEST, formTimeSpace, 0, SpringLayout.WEST, jScrollSchDescriptionKk);
        layout.putConstraint(SpringLayout.NORTH, formTimeSpace, 5, SpringLayout.SOUTH, jScrollSchDescriptionKk);
    }

    private void organiseLayout() {
        SpringLayout layout = new SpringLayout();
        visitPanel.setLayout(layout);
        layout.putConstraint(SpringLayout.WEST, visitDescriptionPanel, 5, SpringLayout.WEST, visitPanel);
        layout.putConstraint(SpringLayout.NORTH, visitDescriptionPanel, 5, SpringLayout.NORTH, visitPanel);

        layout.putConstraint(SpringLayout.WEST, tablePanel, 5, SpringLayout.WEST, visitPanel);
        layout.putConstraint(SpringLayout.NORTH, tablePanel, 5, SpringLayout.SOUTH, visitDescriptionPanel);
    }
}
